<template>
    <div class="hotProduct">
        <div class="customeRow" style="position: relative;">
            <div class="hotProductTitle">
                Billingham 博客-探索更多摄影师的故事
            </div>
            <div class="hotProductContainerRow">
                <div class="hotProductContainer" :style="{'transform': blogObject.translate}">
                    <div class="product" v-for="(item,index) in lists" :key="index"
                         :style="{'left':(index*item.leftWidth)+'%'}">
                        <div class="productImg">
                            <img :src="item.image.file_path" alt="billingham"/>
                        </div>
                        <div class="colorList">
                            {{item.article_title}}
                        </div>
                        <!--                        <div class="productName">Galbin 8 望遠鏡包在路上拍世界的人|旅行摄影师Mechrdad Samak-Abedi专访在路上拍世界的人|旅行摄影师Mechrdad Samak-Abedi专访在路上拍世界的人|旅行摄影师Mechrdad Samak-Abedi专访</div>-->
                        <div :class="[blogObject.currentIndex===index?'productPrice active':'productPrice']"
                             @click="handleRedirectBlogDetail(item.article_id)">了解更多
                        </div>
                    </div>
                </div>
            </div>
            <div class="productNext next" @click="handleLast()">
                <i class="el-icon-arrow-left"></i>
            </div>
            <div class="productNext last" @click="handleNext()">
                <i class="el-icon-arrow-right"></i>
            </div>
        </div>
        <div class="blogBottom">
            <div :class="[blogObject.currentIndex===index?'blogBottomItem active':'blogBottomItem']"
                 v-for="(item,index) in lists"
                 :key="(item+index)">

            </div>
        </div>
    </div>
</template>

<script>
    import Vue from 'vue';
    import {Drawer, Popover} from 'element-ui';

    Vue.use(Drawer);
    Vue.use(Popover);
    export default {
        name: 'HotProduct',
        data: () => {
            return {}
        },
        props: {
            msg: String,
            lists: Array,
            leftWidth: Number,
            blogObject: Object
        },
        mounted() {
        },
        methods: {
            /**
             * 下一个产品
             */
            handleNext() {
                let {leftWidth, lists, blogObject} = this;
                let {clickCount, currentIndex, translate} = blogObject;
                clickCount += 1;
                const len = lists.length;
                const canMove = Math.floor(100 / leftWidth);
                if (currentIndex < (canMove - 1)) {
                    currentIndex = currentIndex + 1;
                    if (currentIndex === len - 1) {
                        currentIndex = 0;
                    }
                    let newBlogObject = {
                        translate,
                        clickCount: 0,
                        currentIndex
                    };
                    this.$emit('handleChangeProductObject', {key: 'blogObject', value: newBlogObject});
                    return;
                }
                currentIndex = currentIndex + 1;
                if (clickCount > (len - canMove)) {
                    translate = 'translate(0)';
                    clickCount = 0;
                    currentIndex = 0;
                } else {
                    const leftAll = clickCount * leftWidth;
                    translate = 'translate(-' + leftAll + '%)';
                }
                let newBlogObject = {
                    translate,
                    clickCount,
                    currentIndex
                };
                this.$emit('handleChangeProductObject', {key: 'blogObject', value: newBlogObject});
            },
            handleLast() {
                let {leftWidth, lists, blogObject} = this;
                let {clickCount, currentIndex} = blogObject;
                let translate = '';
                clickCount -= 1;
                const len = lists.length;
                const canMove = Math.floor(100 / leftWidth);
                if (currentIndex <= canMove - 1 && currentIndex > 0) {
                    currentIndex = currentIndex - 1;
                    let newBlogObject = {
                        translate,
                        clickCount: 0,
                        currentIndex
                    };
                    this.$emit('handleChangeProductObject', {key: 'blogObject', value: newBlogObject});
                    return;
                }
                currentIndex = currentIndex - 1;
                if (clickCount < 0) {
                    translate = 'translate(-' + ((len - canMove) * leftWidth) + '%)';
                    clickCount = len - canMove;
                    currentIndex = len - 1;
                } else {
                    const leftAll = clickCount * leftWidth;
                    translate = 'translate(-' + leftAll + '%)';
                }
                let newBlogObject = {
                    translate,
                    clickCount,
                    currentIndex
                };
                this.$emit('handleChangeProductObject', {key: 'blogObject', value: newBlogObject});
            },
            /**
             * 跳转博客详情
             * @param id
             */
            handleRedirectBlogDetail(id) {
                this.$emit('handleRedirectBlogDetail', {id});
            }
        }
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
    .hotProduct {
        background: #f4f4f4;
        padding-top: 10px;
        margin-top: 40px;
    }

    .hotProductTitle {
        font-weight: bold;
        font-size: 18px;
        padding: 20px 0 40px 20px;
    }

    .hotProductContainerRow {
        overflow: hidden;
        width: 900px;
        margin: 0 auto;
    }

    .hotProductContainer {
        position: relative;
        height: 440px;
        transition: all .3s;
    }

    .hotProductContainer .product {
        width: calc(33.3% - 10px);
        display: inline-block;
        padding: 0 5px;
        position: absolute;
    }

    .productImg {
        width: 100%;
        height: 280px;
        background: #fff;
        display: flex;
        align-items: center;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        border-radius: 2px;
    }

    .colorList {
        font-size: 14px;
        margin-top: 15px;
        font-weight: bold;
        color: #000;
        padding: 0 6px;
        height: 38px;
    }

    .productName {
        font-size: 13px;
        margin-top: 10px;
        padding: 0 6px;
        text-align: justify;
        line-height: 22px;
    }

    .productPrice {
        margin: 8px auto;
        padding: 5px 10px;
        font-size: 13px;
        border: 2px solid orange;
        width: 100px;
        color: orange;
        text-align: center;
        cursor: pointer;
    }

    .productPrice.active {
        background: orange;
        color: #fff;
    }

    .productImg img {
        width: 100%;
    }

    .productNext {
        position: absolute;
        width: 30px;
        height: 30px;
        line-height: 34px;
        background: #000;
        text-align: center;
        top: 334px;
        cursor: pointer;
    }

    .productNext .el-icon-arrow-left:before {
        color: #d8a030;
        font-size: 20px;
    }

    .productNext .el-icon-arrow-right:before {
        color: #d8a030;
        font-size: 20px;
    }

    .productNext.last {
        right: 0;
    }

    .productNext.next {
        left: 0;
    }

    .blogBottom {
        background: #000;
        padding: 20px 0;
        text-align: center;
    }

    .blogBottomItem {
        width: 5px;
        height: 5px;
        -webkit-border-radius: 50%;
        -moz-border-radius: 50%;
        border-radius: 50%;
        background: #fff;
        display: inline-block;
        margin: 0 5px;
    }

    .blogBottomItem.active {
        background: orange;
    }

    @media screen and (max-width: 420px) {
        .customeRow {
            padding: 0 10px;
            width: calc(100% - 20px);
        }

        .hotProductContainerRow {
            width: 100%;

        }

        .hotProductContainer {
            height: 360px;
            padding: 0 10px;
        }

        .productNext {
            top: 295px;
        }

        .productNext.last {
            right: 10px;
        }

        .productNext.next {
            left: 10px;
        }

        .hotProductContainer .product {
            width: 100%;
            padding: 0;
        }

        .productImg {
            height: 240px;
        }

        .productImg img {
            width: 240px;
            margin: 0 auto;
        }
    }

    @media screen and (min-width: 420px) and (max-width: 992px) {
        .hotProductContainerRow {
            width: 100%;
        }

        .productNext.last {
            right: 0;
        }

        .productNext.next {
            left: 0;
        }

        .hotProductContainer .product {
            width: 100%;
            padding: 0;
            text-align: center;
        }

        .productImg img {
            width: 280px;
            margin: 0 auto;
        }
    }
</style>
